package com.dong.design.memento.memento_prototype;

/**
 * @author jianyang206881
 * @date 2018/6/28
 */
public class MementoPatternDemo {

    public static void main(String[] args) throws CloneNotSupportedException {
        Originator originator = new Originator();
        Caretaker caretaker = new Caretaker();
        Hero hero = new Hero();
        hero.setName("ROBINSON");
        hero.setHealth(100);
        hero.setMagic(100);

        originator.setHero(hero);
        // 打了一个怪物,费了10点血，5点魔法
        fightMonster(hero);
        originator.setHero(hero);
        fightMonster(hero);
        originator.setHero(hero);
        // 保存记录
        caretaker.add(originator.saveStateToMemento());

        fightMonster(hero);
        originator.setHero(hero);
        fightMonster(hero);
        originator.setHero(hero);
        fightMonster(hero);
        originator.setHero(hero);
        caretaker.add(originator.saveStateToMemento());

        fightMonster(hero);
        originator.setHero(hero);

        System.out.println("current: " + originator.getHero());
        originator.getStateFromMemento(caretaker.get(1));
        System.out.println("lasted: " + originator.getHero());
        originator.getStateFromMemento(caretaker.get(0));
        System.out.println("earliest: " + originator.getHero());

    }

    private static void fightMonster(Hero hero) {
        hero.setHealth(hero.getMagic() - 10);
        hero.setMagic(hero.getMagic() - 10);
    }

}
